package com.bjesc_b2c.app.appraiset.mvp.model.impl;

import android.content.Context;

import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.model.BasicInfoModel;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.google.gson.Gson;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import master_bean.BasicInfoBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/3/1.
 */

public class BasicInfoModelImpl implements BasicInfoModel {
    private CallBack callBack;

    public BasicInfoModelImpl(CallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void getBasicInfo(Context context) {
        HashMap<String, Object> build = new MapUtil()
                .put("id", CustomSharedPreferences.getString(context, KeyBean.CARID.valueOf(), KeyBean.FILE_NAME.valueOf()))
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).getBasicInfo()
                , new BaseSubscriber<TokenBean<BasicInfoBean>>(context) {
                    @Override
                    public void onSuccess(TokenBean<BasicInfoBean> o) {
                     if(o.getData()!= null ){
                         if(callBack!=null){
                             callBack.callBack(o.getData());
                         }
                     }
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    @Override
    public void saveBasicInfo(Context context, BasicInfoBean bean) {
        String json = new Gson().toJson(bean);
        HashMap<String, Object> build = new MapUtil()
                .put("data", json)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).saveBasicInfo()
                , new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean o) {

                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    public interface CallBack{
        void callBack(BasicInfoBean bean);
    }
}
